﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.WebControls;
using VINETLIFE.BL;

namespace VINETLIFE.Module
{
    public partial class ucVnHomeNewProducts : System.Web.UI.UserControl
    {
        private Cache cache = HttpContext.Current.Cache;
        cmsProductBL proBL = new cmsProductBL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                rptHomeNewDataSource();
        }
        protected int GetLocationId()
        {
            int locationId = 0;
            if (Session["LocationId"] == null)
            {
                locationId = 0;
            }
            else
            {
                locationId = int.Parse(Session["LocationId"].ToString());
            }
            return locationId;
        }
        protected string GetSort()
        {
            string sortBy = "";
            string sort = "prod";
            if (sort.Equals("prd"))
            {
                sortBy = "price desc";
            }
            else if (sort.Equals("pra"))
            {
                sortBy = "price asc";
            }
            else if (sort.Equals("proa"))
            {
                sortBy = "productid asc";
            }
            else if (sort.Equals("prod"))
            {
                sortBy = "productid desc";
            }
            return sortBy;
        }
        public string FriendlyUrl(string s)
        {
            return Ultility.Change_AV(s);
        }
        protected void rptHomeNewDataSource()
        {
            if (cache["HomeNew"] == null)
            {
                DataTable dtCache = new DataView(proBL.SelectFrontPage(20, GetLocationId(), 0, 1, 1, -1, -1, -1), "", GetSort(), DataViewRowState.CurrentRows).ToTable();
                if (dtCache != null)
                    cache.Insert("HomeNew", dtCache, null, DateTime.Now.AddSeconds(150), TimeSpan.Zero);
            }
            rptHomeNew.DataSource = (DataTable)cache["HomeNew"];
            rptHomeNew.DataBind();
        }
        protected void rptHomeProducts_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                DataRowView drv = (DataRowView)e.Item.DataItem;

                bool isNew = (bool)drv["IsNew"];
                bool isPromotion = (bool)drv["IsPromotion"];
                Int64 price = (Int64)drv["Price"];
                Int64 salePrice = (Int64)drv["SalePrice"];
                double percent = 0;
                try
                {
                    percent = (double)(((price - salePrice) * 100) / price);
                }
                catch { percent = 0; }

                Label lblType = (Label)e.Item.FindControl("lblType");
                Panel pnlPromotion = (Panel)e.Item.FindControl("pnlPromotion");
                Panel pnlNonPromotion = (Panel)e.Item.FindControl("pnlNonPromotion");
                Literal ltrSale = (Literal)e.Item.FindControl("ltrSale");
                pnlPromotion.Visible = false;
                pnlNonPromotion.Visible = true;
                if (isNew)
                {
                    lblType.Text = "New";
                    pnlPromotion.Visible = false;
                    pnlNonPromotion.Visible = true;
                }
                if (isPromotion)
                {
                    lblType.Text = "Sale";
                    pnlPromotion.Visible = true;
                    pnlNonPromotion.Visible = false;
                    ltrSale.Text = percent + "%";
                }
                if (isNew && isPromotion)
                {
                    lblType.Text = "Sale";
                    pnlPromotion.Visible = true;
                    pnlNonPromotion.Visible = false;
                    ltrSale.Text = percent + "%";
                }
            }
        }
    }
}